<template>
  <div class="user-list">
    <!-- 面包屑 -->
    <el-row>
      <my-breadcrumb level1="订单管理" level2="订单列表"></my-breadcrumb>
    </el-row>
    <!-- 表格 -->
    <el-row>
      <el-table
        :data="tableData"
        style="width: 100%"
        :default-sort="{ prop: 'create_time', order: 'descending' }"
      >
        <el-table-column type="index" label="#" width="60"></el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="300">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格（元）" width="120">
        </el-table-column>
        <el-table-column prop="order_pay" label="是否付款" width="120">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.order_pay === '1' ? 'primary' : 'danger'"
              >{{ scope.row.order_pay === '1' ? '已付款' : '未付款' }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="80">
        </el-table-column>

        <el-table-column
          label="下单时间"
          width="120"
          prop="create_time"
          sortable
        >
          <template slot-scope="scope">
            {{ scope.row.create_time | fmBaseDateStyle }}
          </template>
        </el-table-column>

        <el-table-column prop="id" label="操作" width="140">
          <template slot-scope="scope">
            <el-row>
              <el-tooltip effect="dark" content="编辑" placement="top">
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  circle
                  plain
                  size="mini"
                  @click="handleEditOrder(scope.row)"
                ></el-button>
              </el-tooltip>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <!-- 分页 -->
    <el-row>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-row>
    <!-- 编辑地址 -->
    <order-address-edit
      :dialogVisibleEdit="dialogVisibleEdit"
      :orderInfo="currOrderInfo"
      @editClosed="dialogVisibleEdit = false"
      @editSuccess="getOrderListData"
    ></order-address-edit>
  </div>
</template>

<script>
import MyBreadcrumb from '@/components/MyBreadcrumb'
import OrderAddressEdit from '@/views/order/components/OrderAddressEdit'
export default {
  components: {
    MyBreadcrumb,
    OrderAddressEdit,
  },
  data() {
    return {
      query: '',
      tableData: [],
      total: 0,
      pageSize: 5,
      pageNum: 1,
      dialogVisibleEdit: false,
      currOrderInfo: null,
    }
  },
  created() {
    this.getOrderListData()
  },
  computed: {},
  methods: {
    async getOrderListData() {
      const { data } = await this.$http.get('/orders', {
        params: {
          query: this.query,
          pagenum: this.pageNum,
          pagesize: this.pageSize,
        },
      })

      if (data.meta.status === 200) {
        const { total, goods } = data.data
        this.tableData = goods
        this.total = total
      } else {
        this.$message.warning(data.meta.msg)
      }
    },

    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageSize = val
      // 切换每页数据条目数量的时候，页码切回到 1
      this.pageNum = 1
      this.getOrderListData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.pageNum = val
      this.getOrderListData()
    },

    // 编辑订单地址
    handleEditOrder(orderInfo) {
      this.dialogVisibleEdit = true
      this.currOrderInfo = JSON.parse(JSON.stringify(orderInfo))
    },
  },
}
</script>

<style lang="less" scoped>
.user-list {
  .el-row {
    padding: 5px 0;
  }
}
</style>
